package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.dto.*;
import cn.zjutleo.village_server.exception.apiException.daoException.SelectException;
import cn.zjutleo.village_server.service.CooperateService;
import cn.zjutleo.village_server.vo.CooperateVo;
import cn.zjutleo.village_server.vo.PolicyVo;
import cn.zjutleo.village_server.vo.ResponseBean;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author ：杜雨茜
 * @Description ：村民互助活动
 * @Date ：2021/12/19 18:35
 **/
@Api(tags = "村民互助活动管理模块")
@RestController
@RequestMapping("/api/cooperate")
public class CooperationController {
    @Resource
    private CooperateService cooperateService;

    @ApiOperation("添加活动")
    @PostMapping("/add")
    private ResponseBean<String> addCooperate(@RequestBody CooperateDto cooperateDto){
        CooperateVo cooperateVo = new CooperateVo();
        BeanUtils.copyProperties(cooperateDto,cooperateVo);
        cooperateService.saveCooperate(cooperateVo);
        return new ResponseBean<>("success");
    }

    @ApiOperation("更新活动")
    @PostMapping("/update")
    private ResponseBean<String> updateCooperate(@RequestBody CooperateDto cooperateDto){
        CooperateVo cooperateVo = new CooperateVo();
        BeanUtils.copyProperties(cooperateDto,cooperateVo);
        cooperateService.updateCooperateById(cooperateDto);
        return new ResponseBean<>("success");
    }

    @ApiOperation("更新活动审核状态")
    @PostMapping("/update/check")
    private ResponseBean<String> updateCooperateStateCheck(@RequestBody CooperateCheckDto cooperateCheckDto){
        cooperateService.updateCooperateByCheck(cooperateCheckDto);
        return new ResponseBean<>("success");
    }

    @ApiOperation("获取活动信息")
    @PostMapping("/get")
    private ResponseBean<CooperateDto> getCooperate(@RequestBody ID id) {
        CooperateDto cooperateDto = new CooperateDto();
        try {
            CooperateVo cooperateVo = cooperateService.getExistCooperateById(id.getId());
            BeanUtils.copyProperties(cooperateVo, cooperateVo);
        } catch (SelectException e) {
            e.printStackTrace();
            return new ResponseBean(200, e.getMessage(), null);
        }
        return new ResponseBean<>(cooperateDto);
    }

    @ApiOperation("删除活动")
    @PostMapping("/delete")
    private ResponseBean<String> deleteCooperate(@RequestBody ID id) {
        cooperateService.deleteCooperateById(id.getId());
        return new ResponseBean<>("success");
    }

    @PostMapping("/page")
    private ResponseBean<List<CooperateDto>> selectPolicyList(@RequestBody CooperateSelectDto cooperateSelectDto) {
        List<CooperateDto> result = cooperateService.selectCooperateList(cooperateSelectDto);
        return new ResponseBean<>(result);
    }

    @PostMapping("/pageList/{start}/{limit}")
    private ResponseBean<PageInfo<CooperateDto>> selectPolicyPage(@RequestBody CooperateSelectDto cooperateSelectDto, @PathVariable("start") Integer start, @PathVariable("limit") Integer limit) {
        PageInfo<CooperateDto> result = cooperateService.selectCooperatePage(cooperateSelectDto,start,limit);
        return new ResponseBean<>(result);
    }

}
